﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Ecommerce.EntityFrameworkCore;
using Ecommerce.tbCommodity;

namespace Ecommerce.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class tbOrderInfoesController : ControllerBase
    {
        private readonly EcommerceDbContext _context;

        public tbOrderInfoesController(EcommerceDbContext context)
        {
            _context = context;
        }

        // GET: api/tbOrderInfoes
        [HttpGet]
        public async Task<ActionResult<IEnumerable<tbOrderInfo>>> GettbOrderInfos()
        {
            return await _context.tbOrderInfos.ToListAsync();
        }

        // GET: api/tbOrderInfoes/5
        [HttpGet("{id}")]
        public async Task<ActionResult<tbOrderInfo>> GettbOrderInfo(Guid id)
        {
            var tbOrderInfo = await _context.tbOrderInfos.FindAsync(id);

            if (tbOrderInfo == null)
            {
                return NotFound();
            }

            return tbOrderInfo;
        }

        // PUT: api/tbOrderInfoes/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PuttbOrderInfo(Guid id, tbOrderInfo tbOrderInfo)
        {
            if (id != tbOrderInfo.Id)
            {
                return BadRequest();
            }

            _context.Entry(tbOrderInfo).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!tbOrderInfoExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/tbOrderInfoes
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<tbOrderInfo>> PosttbOrderInfo(tbOrderInfo tbOrderInfo)
        {
            _context.tbOrderInfos.Add(tbOrderInfo);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (tbOrderInfoExists(tbOrderInfo.Id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtAction("GettbOrderInfo", new { id = tbOrderInfo.Id }, tbOrderInfo);
        }

        // DELETE: api/tbOrderInfoes/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeletetbOrderInfo(Guid id)
        {
            var tbOrderInfo = await _context.tbOrderInfos.FindAsync(id);
            if (tbOrderInfo == null)
            {
                return NotFound();
            }

            _context.tbOrderInfos.Remove(tbOrderInfo);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool tbOrderInfoExists(Guid id)
        {
            return _context.tbOrderInfos.Any(e => e.Id == id);
        }
    }
}
